package com.wx.interceptor;

import com.wx.consts.UserConstant;
import org.springframework.web.servlet.HandlerInterceptor;
import org.springframework.web.servlet.ModelAndView;

import javax.servlet.http.HttpServletRequest;
import javax.servlet.http.HttpServletResponse;

/**
 * Created by Max on 2018/4/13.
 */
public class LoginInterceptor implements HandlerInterceptor {
    @Override
    public boolean preHandle(HttpServletRequest request, HttpServletResponse response, Object handler) throws Exception {
        //获取请求的url
        String url = request.getRequestURI();
        //这里公开地址是登陆提交的地址
        if(url.contains("login") || url.contains("doLogin")){
            System.out.println("用户进行登录-->不拦截");
            //如果进行登陆提交，放行
                return true;
        }

        if(request.getSession().getAttribute(UserConstant.USER_INFO)!=null){
            System.out.println("用户已登录");
            return true;
        }else{
            System.out.println("用户未登录-->跳转到登录页面");
            request.getRequestDispatcher("/WEB-INF/views/login.jsp").forward(request, response);
        }
        return false;
    }

    @Override
    public void postHandle(HttpServletRequest request, HttpServletResponse response, Object handler, ModelAndView modelAndView) throws Exception {

    }

    @Override
    public void afterCompletion(HttpServletRequest request, HttpServletResponse response, Object handler, Exception e) throws Exception {

    }
}
